import {ModalForm, ProFormText} from "@ant-design/pro-components";
import {DictDataVO} from "@/services/DictDataService";
import {useRef, useState} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";

export interface DictDataUpdateModalProps {
    onFinish: (formData: any) => Promise<boolean | void>;
    dictData?: DictDataVO;
    open: boolean;
    onOpenChange: (state: boolean) => void;
}

const DictDataUpdateModal = (props: DictDataUpdateModalProps) => {
    const formRef = useRef<ProFormInstance>()
    const [inst, setInst] = useState<DictDataVO | undefined>()

    const onOpenChange = (state: boolean) => {
        if (state) {
            setInst(props.dictData || undefined)
            formRef?.current?.setFieldsValue(props.dictData)
        } else {
            setInst(undefined)
            formRef?.current?.resetFields()
        }
        props.onOpenChange(state);
    }
    return <>
        <ModalForm
            formRef={formRef}
            title={props.dictData ? "修改字典数据" : "添加字典数据"}
            open={props.open}
            initialValues={inst}
            onFinish={props.onFinish}
            onOpenChange={onOpenChange}
            >
            <ProFormText
                label="字典编码"
                hidden
                name="id"
            />
            <ProFormText
                label="字典排序"
                name="sort"
            />
            <ProFormText
                label="字典标签"
                name="label"
            />
            <ProFormText
                label="字典键值"
                name="value"
            />
            <ProFormText
                label="字典类型"
                name="dictType"
            />
            <ProFormText
                label="状态（0正常 1停用）"
                name="status"
            />
            <ProFormText
                label="颜色类型"
                name="colorType"
            />
            <ProFormText
                label="css 样式"
                name="cssClass"
            />
            <ProFormText
                label="备注"
                name="remark"
            />
        </ModalForm>
    </>
}

export default DictDataUpdateModal
